package net.lwf.hibernate.util;

import net.lwf.exception.ExceptionUtil;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;


public class HibernateUtil {
	
    private static SessionFactory sessionFactory = null;

    private static SessionFactory buildSessionFactory() {
        try {
            // Create the SessionFactory from hibernate.cfg.xml
           return new Configuration().configure().buildSessionFactory();
        }
        catch (Throwable throwable) {
        	throwable.printStackTrace();
        	ExceptionUtil.logAndThrowException(HibernateUtil.class, throwable);
        }
        return null;
    }

    public static SessionFactory getSessionFactory() {
    	if(sessionFactory == null) {
    		sessionFactory = buildSessionFactory();
    	}
        return sessionFactory;
    }
    
    public static void shutdown() {
    	// Close caches and connection pools
    	getSessionFactory().close();
    }
}